package domain.entities;

import org.hibernate.LockMode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: vgonzalez
 * Date: 01/11/12
 * Time: 13:36
 * To change this template use File | Settings | File Templates.
 */
public class ExternalValidationDAO extends HibernateDaoSupport {
    private static final Logger log = LoggerFactory
            .getLogger(ExternalValidationDAO.class);
    // property constants

    protected void initDao() {
        // do nothing
    }

    public void save(ExternalValidation transientInstance) {
        log.debug("saving ExternalValidation instance");
        try {
            getHibernateTemplate().save(transientInstance);
            log.debug("save successful");
        } catch (RuntimeException re) {
            log.error("save failed", re);
            throw re;
        }
    }

    public void delete(ExternalValidation persistentInstance) {
        log.debug("deleting ExternalValidation instance");
        try {
            getHibernateTemplate().delete(persistentInstance);
            log.debug("delete successful");
        } catch (RuntimeException re) {
            log.error("delete failed", re);
            throw re;
        }
    }

    public ExternalValidation findById(java.lang.Integer id) {
        log.debug("getting ExternalValidation instance with id: " + id);
        try {
            ExternalValidation instance = (ExternalValidation) getHibernateTemplate().get(
                    "domain.entities.ExternalValidation", id);
            return instance;
        } catch (RuntimeException re) {
            log.error("get failed", re);
            throw re;
        }
    }

    public List<ExternalValidation> findByExample(ExternalValidation instance) {
        log.debug("finding ExternalValidation instance by example");
        try {
            List<ExternalValidation> results = (List<ExternalValidation>) getHibernateTemplate()
                    .findByExample(instance);
            log.debug("find by example successful, result size: "
                    + results.size());
            return results;
        } catch (RuntimeException re) {
            log.error("find by example failed", re);
            throw re;
        }
    }

    public List findByProperty(String propertyName, Object value) {
        log.debug("finding ExternalValidation instance with property: " + propertyName
                + ", value: " + value);
        try {
            String queryString = "from ExternalValidation as model where model."
                    + propertyName + "= ?";
            return getHibernateTemplate().find(queryString, value);
        } catch (RuntimeException re) {
            log.error("find by property name failed", re);
            throw re;
        }
    }

    public List findAll() {
        log.debug("finding all ExternalValidation instances");
        try {
            String queryString = "from ExternalValidation";
            return getHibernateTemplate().find(queryString);
        } catch (RuntimeException re) {
            log.error("find all failed", re);
            throw re;
        }
    }

    public ExternalValidation merge(ExternalValidation detachedInstance) {
        log.debug("merging ExternalValidation instance");
        try {
            ExternalValidation result = (ExternalValidation) getHibernateTemplate().merge(
                    detachedInstance);
            log.debug("merge successful");
            return result;
        } catch (RuntimeException re) {
            log.error("merge failed", re);
            throw re;
        }
    }

    public void attachDirty(ExternalValidation instance) {
        log.debug("attaching dirty ExternalValidation instance");
        try {
            getHibernateTemplate().saveOrUpdate(instance);
            log.debug("attach successful");
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            throw re;
        }
    }

    public void attachClean(ExternalValidation instance) {
        log.debug("attaching clean ExternalValidation instance");
        try {
            getHibernateTemplate().lock(instance, LockMode.NONE);
            log.debug("attach successful");
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            throw re;
        }
    }

    public static ExternalValidationDAO getFromApplicationContext(ApplicationContext ctx) {
        return (ExternalValidationDAO) ctx.getBean("ExternalValidationDAO");
    }
}